package com.yungu.swift.lbs.trace.gaode;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.Lists;
import com.yungu.swift.utils.HttpUtils;
import com.yungu.swift.utils.JsonUtils;
import com.yungu.swift.utils.ParamUtil;
import com.yungu.swift.utils.StringUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 高德猎鹰终端管理
 *
 * @author : cuixiuyin
 * @date : 2019/5/27
 */
@Slf4j
public class TerminalUtils {

    private static final Integer SUCCESS_CODE = 10000;
    private static final String SUCCESS_MSG = "OK";

    /**
     * 创建终端
     *
     * @param key  高德key - 必填
     * @param sid  高德sid - 必填
     * @param name 终端名字 - 必填
     * @return
     */
    public static Long createTerminal(String key, String sid, String name) {
        if (ParamUtil.isIllegal(key, sid, name)) {
            return 0L;
        }
        Map<String, Object> paramMap = new HashMap<>(4);
        paramMap.put("key", key);
        paramMap.put("sid", sid);
        paramMap.put("name", name);
        Map<String, Object> objectMap = HttpUtils.post("https://tsapi.amap.com/v1/track/terminal/add", paramMap);
        Object result = objectMap.get("result");
        if (result != null) {
            try {
                JsonNode jsonNode = JsonUtils.readNode(result.toString());
                int errcode = jsonNode.get("errcode").asInt();
                String errmsg = jsonNode.get("errmsg").asText();
                if (errcode == SUCCESS_CODE && SUCCESS_MSG.equals(errmsg)) {
                    JsonNode data = jsonNode.get("data");
                    JsonNode tid = data.get("tid");
                    if (tid != null) {
                        return tid.asLong();
                    }
                }
            } catch (IOException e) {
                log.error("TerminalUtils createTerminal error,result:{}", result, e);
            }
        }
        return 0L;
    }


    /**
     * 删除终端
     *
     * @param key
     * @param sid
     * @param tid
     * @return
     */
    public static Boolean delTerminal(String key, String sid, Long tid) {
        if (ParamUtil.isIllegal(key, sid, tid)) {
            return Boolean.FALSE;
        }
        Map<String, Object> paramMap = new HashMap<>(4);
        paramMap.put("key", key);
        paramMap.put("sid", sid);
        paramMap.put("tid", tid);
        Map<String, Object> objectMap = HttpUtils.post("tps://tsapi.amap.com/v1/track/terminal/delete", paramMap);
        Object result = objectMap.get("result");
        if (result != null) {
            try {
                JsonNode jsonNode = JsonUtils.readNode(result.toString());
                int errcode = jsonNode.get("errcode").asInt();
                String errmsg = jsonNode.get("errmsg").asText();
                if (errcode == SUCCESS_CODE && SUCCESS_MSG.equals(errmsg)) {
                    return Boolean.TRUE;
                }
            } catch (IOException e) {
                log.error("TerminalUtils delTerminal error,result:{}", result, e);
            }
        }
        return Boolean.FALSE;
    }

    /**
     * 查询终端
     *
     * @param key  高德key - 必填
     * @param sid  高德sid - 必填
     * @param tid  终端id - 非必填
     * @param name 终端 name - 非必填
     * @param page 当前显示页数 - 每页显示50条数据 - 非必填
     * @return
     */
    public static List<TerminalInfo> listTerminal(String key, String sid, Long tid, String name, Integer page) {
        if (ParamUtil.isIllegal(key, sid)) {
            return Lists.newArrayList();
        }
        StringBuffer stringBuffer = new StringBuffer("?");
        stringBuffer.append("key=" + key + "&");
        stringBuffer.append("sid=" + sid + "&");
        if (tid != null) {
            stringBuffer.append("tid=" + tid + "&");
        }
        if (name != null) {
            stringBuffer.append("name=" + name + "&");
        }
        if (page != null) {
            stringBuffer.append("page=" + page + "&");
        }
        Map<String, Object> objectMap = HttpUtils.get("https://tsapi.amap.com/v1/track/terminal/list" +
                StringUtils.removeEnd(stringBuffer.toString(), "&"));
        Object result = objectMap.get("result");
        if (result != null) {
            try {
                JsonNode jsonNode = JsonUtils.readNode(result.toString());
                int errcode = jsonNode.get("errcode").asInt();
                String errmsg = jsonNode.get("errmsg").asText();
                if (errcode == SUCCESS_CODE && SUCCESS_MSG.equals(errmsg)) {
                    JsonNode data = jsonNode.get("data");
                    if(data != null) {
                        JsonNode results = data.get("results");
                        if (results != null) {
                            return JsonUtils.readListValue(results.toString(), TerminalInfo.class);
                        }
                    }
                }
            } catch (IOException e) {
                log.error("TerminalUtils listTerminal error,result:{}", result, e);
            }
        }
        return Lists.newArrayList();
    }


    @Data
    public static class TerminalInfo {

        /**
         * 终端创建时间
         */
        private Date createtime;

        /**
         * 终端名字
         */
        private String name;

        /**
         * 终端 pid
         */
        private Long tid;
    }

}
